package com.hemaapp.yjnh.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.hm_FrameWork.result.HemaPageArrayResult;
import com.hemaapp.hm_FrameWork.view.RefreshLoadmoreLayout;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.adapter.GuideAdapter;
import com.hemaapp.yjnh.bean.Guide;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import xtom.frame.util.XtomToastUtil;
import xtom.frame.view.XtomListView;
import xtom.frame.view.XtomRefreshLoadmoreLayout;

/**
 * 新手指南
 */
public class GuideActivity extends BaseActivity {

    @Bind(R.id.title_right_btn)
    Button titleRightBtn;
    @Bind(R.id.title_text)
    TextView titleText;
    @Bind(R.id.listview)
    XtomListView listView;
    @Bind(R.id.layout_refresh)
    RefreshLoadmoreLayout layoutRefresh;
    int page = 0;
    ArrayList<Guide> mDatas = new ArrayList<>();
    GuideAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_fresh_man_help);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
        getNetWorker().getGuideList(page+"");
    }

    @Override
    protected void setListener() {
        titleText.setText("新手指南");
        titleRightBtn.setVisibility(View.GONE);
        layoutRefresh.setOnStartListener(new XtomRefreshLoadmoreLayout.OnStartListener() {
            @Override
            public void onStartRefresh(XtomRefreshLoadmoreLayout v) {
                page = 0;
                getNetWorker().getGuideList(page+"");
            }

            @Override
            public void onStartLoadmore(XtomRefreshLoadmoreLayout v) {
                page++;
                getNetWorker().getGuideList(page+"");
            }
        });
    }

    @OnClick(R.id.title_left_btn)
    public void onClick() {
        finish();
    }

    @Override
    protected void callBeforeDataBack(HemaNetTask hemaNetTask) {
        BaseHttpInformation info = (BaseHttpInformation) hemaNetTask
                .getHttpInformation();
        switch (info) {
            case GUIDE_LIST:
                showProgressDialog("请稍候");
                break;
        }
    }

    @Override
    protected void callAfterDataBack(HemaNetTask hemaNetTask) {
        BaseHttpInformation info = (BaseHttpInformation) hemaNetTask
                .getHttpInformation();
        switch (info) {
            case GUIDE_LIST:
                cancelProgressDialog();
                break;
        }
    }

    @Override
    protected void callBackForServerSuccess(HemaNetTask netTask, HemaBaseResult baseResult) {
        BaseHttpInformation info = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (info) {
            case GUIDE_LIST:
                String page = netTask.getParams().get("page");
                HemaPageArrayResult<Guide> bResult = (HemaPageArrayResult<Guide>) baseResult;
                if (bResult == null || bResult.getObjects() == null)
                    return;
                if ("0".equals(page)) {// 刷新
                    layoutRefresh.refreshSuccess();
                    layoutRefresh.setLoadmoreable(true);
                    mDatas.clear();
                    mDatas.addAll(bResult.getObjects());
                } else {// 更多
                    layoutRefresh.loadmoreSuccess();
                    if (bResult.getObjects().size() > 0) {
                        mDatas.addAll(bResult.getObjects());
                    } else {
                        layoutRefresh.setLoadmoreable(false);
                        XtomToastUtil.showShortToast(mContext, "已经到最后啦");
                    }
                }
                refreshDatas(-1);
                break;
        }
    }

    @Override
    protected void callBackForServerFailed(HemaNetTask hemaNetTask, HemaBaseResult hemaBaseResult) {
        BaseHttpInformation information = (BaseHttpInformation) hemaNetTask.getHttpInformation();
        switch (information) {
            case GUIDE_LIST:
                XtomToastUtil.showShortToast(mContext, "获取数据失败," + hemaBaseResult.getMsg());
                String page = hemaNetTask.getParams().get("page");
                if ("0".equals(page))// 刷新
                    layoutRefresh.refreshFailed();
                else
                    layoutRefresh.loadmoreFailed();
                break;
        }
    }

    @Override
    protected void callBackForGetDataFailed(HemaNetTask hemaNetTask, int i) {
        BaseHttpInformation information = (BaseHttpInformation) hemaNetTask
                .getHttpInformation();
        switch (information) {
            case GUIDE_LIST:
                XtomToastUtil.showShortToast(mContext, "获取数据失败,请稍后再试");
                String page = hemaNetTask.getParams().get("page");
                if ("0".equals(page))// 刷新
                    layoutRefresh.refreshFailed();
                else
                    layoutRefresh.loadmoreFailed();
                refreshDatas(i);
                break;
        }
    }

    private void refreshDatas(int failedType) {
        if (null == mAdapter) {
            mAdapter = new GuideAdapter(mContext, mDatas);
            mAdapter.setEmptyString("暂无内容");
            listView.setAdapter(mAdapter);
        }else {
            mAdapter.notifyDataSetChanged();
        }
        mAdapter.setFailtype(failedType);
    }
}
